package com.ruoyi.web.controller.purchase;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.purchase.domain.Firm;
import com.ruoyi.purchase.service.FirmService;
import com.ruoyi.system.domain.SysCity;
import com.ruoyi.system.domain.SysProvince;
import io.swagger.models.auth.In;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("purchase/firm")
public class FirmController extends BaseController {
    private String prefix = "purchase/firm";
    @Autowired
    private FirmService firmService;
    @RequiresPermissions("purchase:firm:view")
    @GetMapping()
    public String Firm()
    {
        return prefix + "/firm";
    }
    /**
     * 查询厂商信息列表
     */
    @RequiresPermissions("purchase:firm:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Firm firm)
    {
        startPage();
        List<Firm> list = firmService.queryList(firm);
        return getDataTable(list);
    }

    /**
     * 导出厂商信息列表
     */
    @RequiresPermissions("purchase:firm:export")
    @Log(title = "品牌信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Firm firm)
    {
        List<Firm> list = firmService.queryList(firm);
        ExcelUtil<Firm> util = new ExcelUtil<Firm>(Firm.class);
        return util.exportExcel(list, "firm");
    }

    /**
     * 增加厂商信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存厂商信息
     */
    @RequiresPermissions("purchase:firm:add")
    @Log(title = "厂商信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Firm firm)
    {
        return toAjax(firmService.save(firm));
    }

    /**
     * 修改厂商信息
     */
    @GetMapping("/edit/{fid}")
    public String edit(@PathVariable("fid") Long firmId, ModelMap mmap)
    {
        Firm firm = firmService.getById(firmId);
        mmap.put("firm", firm);
        SysUser user = getSysUser();//当前登录用户信息
        mmap.put("user",user);
        return prefix + "/edit";
    }

    /**
     * 修改保存厂商信息
     */
    @RequiresPermissions("purchase:firm:edit")
    @Log(title = "品牌信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Firm firm)
    {
        return toAjax(firmService.updateById(firm));
    }

    /**
     * 删除厂商信息
     */
    @RequiresPermissions("purchase:firm:remove")
    @Log(title = "品牌信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(firmService.removeByIds(Arrays.asList(ids)));
    }

    /**
     * 获取城市
     * @param cid
     * @return
     */
    @GetMapping("/city/select")
    @ResponseBody
    public List<SysCity> select(Integer cid)
    {
        List<SysCity> list = firmService.selectCityById(cid);
        return  list;
    }

    /**
     * 获取省
     * @return
     */
    @GetMapping("/province/select")
    @ResponseBody
    public List<SysProvince> select()
    {
        List<SysProvince> list = firmService.selectProv();
        return list;
    }

    /**
     * 用户状态修改
     */
    @Log(title = "厂商管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions("purchase:firm:edit")
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(Firm firm)
    {
        return toAjax(firmService.changeStatus(firm));
    }

    /**
     * 详情
     * @param firmId
     * @param mmap
     * @return
     */
    @RequiresPermissions("purchase:firm:detail")
    @GetMapping("/detail/{firmId}")
    public String detail(@PathVariable("firmId") Integer firmId, ModelMap mmap)
    {
        SysUser user = getSysUser();//当前登录用户信息
        mmap.put("user",user);
        mmap.put("firm", firmService.selectFirmById(firmId));
        return prefix + "/detail";
    }


}
